package com.demo.singleton;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @Author: shubin_yao
 * @Date: 2022/05/10/22:19
 * @Description: CAS「AtomicReference」(线程安全)
 *
 * 验证不出来?????????
 */
public class Singleton_06 {
    private static final AtomicReference<Singleton_06> INSTANCE = new AtomicReference<>();
    private static Singleton_06 instance;

    private Singleton_06() {
        System.out.println("=======CAS「AtomicReference」(线程安全),构造方法Singleton_06");
    }

    public static final Singleton_06 getInstance() {
        for (; ; ) {
            Singleton_06 instance = INSTANCE.get();
            if (null != instance) return instance;
            INSTANCE.compareAndSet(instance, new Singleton_06());
            return INSTANCE.get();
        }
    }

}
